package com.pro.controller;

import com.pro.pojo.JsClosure;
import com.pro.pojo.JsUnseal;
import com.pro.pojo.JsonData;
import com.pro.pojo.User;
import com.pro.service.JsClosureService;
import com.pro.service.JsUnsealService;
import com.pro.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Date;

@Controller
@RequestMapping("/JsClosure")
public class JsClosureController {

    @Autowired
    private JsClosureService jcs;

    @Autowired
    private UserService userService;

    @Autowired
    private JsUnsealService jsUnsealService;

    //获取封停用户
    @RequestMapping("/getJsClosureList")
    @ResponseBody
    public JsonData getJsClosureList(JsClosure jsClosure){
        List<JsClosure> jsClosureList = jcs.getJsClosureList(jsClosure);
        return jsClosureList!=null?JsonData.buildSuccess(jsClosureList):JsonData.buildError("服务器出错了");
    }

    //封停用户高级检索
    @RequestMapping("/getJsClosureListRetrieval")
    @ResponseBody
    public JsonData getJsClosureListRetrieval(JsClosure jsClosure){
        List<JsClosure> jsClosureList = jcs.getJsClosureListRetrieval(jsClosure);
        return jsClosureList!=null?JsonData.buildSuccess(jsClosureList):JsonData.buildError("服务器出错了");
    }

    //删除
    @RequestMapping("/deleteJsClosureList")
    @ResponseBody
    public JsonData deleteJsClosureList(JsClosure jsClosure){
        boolean b = jcs.deleteJsClosureList(jsClosure);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }

    //添加封停列表
    @RequestMapping("/addshutdowmJsBeforeUser")
    @ResponseBody
    public JsonData addshutdowmJsBeforeUser(JsClosure jsClosure){
        boolean b = jcs.addshutdowmJsBeforeUser(jsClosure);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }

    //解封删除记录
    @RequestMapping("/deleteshutdowmJsClosure")
    @ResponseBody
    public JsonData deleteshutdowmJsClosure(Integer userId){
        boolean b = jcs.deleteshutdowmJsClosure(userId);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }

    //添加解封记录
    @RequestMapping("/addJsUnsealLog")
    @ResponseBody
    public JsonData addJsUnsealLog(JsUnseal jsUnseal){
        boolean b = jcs.addJsUnsealLog(jsUnseal);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }

    //获取所有用户的解封时间
    @RequestMapping("/getJsClosureEndDate")
    @ResponseBody
    public JsonData getJsClosureEndDate(JsClosure jsClosure){
        List<JsClosure> jsClosureEndDate = jcs.getJsClosureEndDate(jsClosure);
        return jsClosureEndDate!=null?JsonData.buildSuccess(jsClosureEndDate):JsonData.buildError("服务器出错了");
    }

    //自动解封用户
    @RequestMapping("/unsealJsBeforeUser")
    @ResponseBody
    public JsonData unsealJsBeforeUser(Integer userId){
        boolean b = jcs.unsealJsBeforeUser(userId);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }

    //解封用户后查看用户的信息
    @RequestMapping("/unsealJsBeforeUserList")
    @ResponseBody
    public JsonData unsealJsBeforeUserList(Integer userId){
        List<JsClosure> jsClosures = jcs.unsealJsBeforeUserList(userId);
        return jsClosures!=null?JsonData.buildSuccess(jsClosures):JsonData.buildError("服务器出错了");
    }

    //添加后台人员封禁记录
    @RequestMapping(value = "/addClosureUser")
    @ResponseBody
    public JsonData addClosureUser(JsClosure jsClosure){
        boolean addClosureUser = jcs.addClosureUser(setEndTime(jsClosure));
        return addClosureUser ? JsonData.buildSuccess("封禁成功"):JsonData.buildError("服务器出错了");
    }

    private JsClosure setEndTime(JsClosure jsClosure){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime lf = LocalDateTime.parse(jsClosure.getStartDate(), df);
        String endDate = df.format(lf.plusDays(jsClosure.getDays()));
        jsClosure.setEndDate(endDate);
        return jsClosure;
    }

    //删除封禁记录
    @RequestMapping(value = "/deleteClosureUser")
    @ResponseBody
    public JsonData deleteClosureUser(Integer userId){
        boolean deleteClosureUser = jcs.deleteClosureUser(userId);
        return deleteClosureUser ? JsonData.buildSuccess("成功"):JsonData.buildError("服务器出错了");
    }


}
